<?php
class Session_model extends MY_Model
{
	public
		$table = 'sessions_admins',
		$prKey = 'id'
	;

	function __construct()
	{
		parent::__construct();
	}

	function insert($data = array())
	{
		$sql = "
			INSERT INTO ".$this->table." (`key`, lastseen, expire, ipaddress, agnt_id)
			VALUES (".$this->db->escape($data['key']).",'".MYSQL_NOW()."', '".date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 365)."',".$this->db->escape($this->input->ip_address()).",".$this->db->escape($this->input->user_agent()).")";
		$res = $this->db->query($sql);
	}
	
	function getSessionByKey($session_key = null)
	{ 
		$user = array();
		$sql = "
			SELECT ".$this->table.".id, ".$this->table.".key, ".$this->table.".lastseen, ".$this->table.".expire, ".$this->table.".ipaddress, ".$this->table.".id_user, ".$this->table.".agnt_id, ".$this->table.".data
			FROM ".$this->table."
			LEFT JOIN admins ON ".$this->table.".id_user=admins.id
			WHERE ".$this->table.".key=".$this->db->escape($session_key);
		$res = $this->db->query($sql);
		$user = $res->row_array();

		if ($res->num_rows() === 1)
		{
			return $user;
		}
	}
}
?>